package com.yupi.aicodehelper.ai;

import com.yupi.aicodehelper.ai.dto.ChatSessionMapper;
import com.yupi.aicodehelper.ai.dto.MonitoringStreamingChatModel;
import com.yupi.aicodehelper.ai.store.RedisChatMemoryStore;
import com.yupi.aicodehelper.config.DataConfig;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.splitter.DocumentByParagraphSplitter;
import dev.langchain4j.data.document.splitter.DocumentSplitters;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class AiCodeHelperServiceFactory {

    // 注入大模型调用 Service
    @Autowired
    private AiCodeHelper aiCodeHelper;
    // 注入 Mapper/Repository
    @Autowired
    private ChatSessionMapper chatSessionMapper;

    @Bean
    public AiCodeHelperService aiCodeHelperService() {
        // 会话记忆
        ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);
        // 使用 Redis + MySQL 存储
        RedisChatMemoryStore store = new RedisChatMemoryStore(DataConfig.jedisPool(), aiCodeHelper, chatSessionMapper);

        ChatMemoryProvider chatMemoryProvider = memoryId -> MessageWindowChatMemory.builder()
                .id(memoryId)
                .maxMessages(10)
                .chatMemoryStore(store)
                .build();

        // 配置 DeepSeek Chat Model
        OpenAiChatModel deepSeekChatModel = OpenAiChatModel.builder()
                .apiKey("sk-a1425efc474048ad979e3bcef3feb210")  // 建议放到环境变量
                .baseUrl("https://api.deepseek.com/v1")        // 指定 DeepSeek API 地址
                .modelName("deepseek-chat")                 // DeepSeek 提供的模型名
                .build();

        // 配置 Streaming Chat Model
        OpenAiStreamingChatModel deepSeekStreamingChatModel = OpenAiStreamingChatModel.builder()
                .apiKey("sk-a1425efc474048ad979e3bcef3feb210")
                .baseUrl("https://api.deepseek.com/v1")
                .modelName("deepseek-chat")
                .build();

        // 包一层性能监控
        StreamingChatModel monitoringModel = new MonitoringStreamingChatModel(deepSeekStreamingChatModel, 800, 300);

        // 构造 AI Service
        AiCodeHelperService aiCodeHelperService = AiServices.builder(AiCodeHelperService.class)
                .chatModel(deepSeekChatModel)
                .streamingChatModel(monitoringModel)
                .chatMemoryProvider(chatMemoryProvider) // 每个会话独立存储
                .build();
        return aiCodeHelperService;
    }
}
